package com.qwiki.test;

import java.io.File;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;

import com.qwiki.dict.CategoryDictionary;
import com.qwiki.dict.TextDictionary;
import com.qwiki.xml.PageFilter;
import com.qwiki.xml.PageHandler;
import com.qwiki.xml.PageMultiHandler;
import com.qwiki.xml.TextPageAsyncHandler;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.xml.sax.SAXException;

public class text2opennlp {
  public static void main(String[] args) throws SAXException, IOException, InterruptedException {
    Logger.getRootLogger()
        .addAppender(new ConsoleAppender(new PatternLayout("%d{yyyy/MM/dd HH:mm:ss.SSS} %p [%c{1}] [%x] %m%n")));


    Options options = new Options();

    Option option = OptionBuilder.create("textify");
    option.setRequired(true);
    option.setArgs(1);
    option.setDescription("the output of textify");
    options.addOption(option);

    option = OptionBuilder.create("numThreads");
    option.setRequired(true);
    option.setArgs(1);
    option.setDescription("the number of threads we want to run in parallel");
    options.addOption(option);

    option = OptionBuilder.create("output");
    option.setRequired(true);
    option.setArgs(1);
    option.setDescription("output file name");
    options.addOption(option);

    CommandLineParser cliParser = new BasicParser();

    String textify = null;
    int numThreads = 1;
    String outputFileName = null;
    PrintStream printer = null;

    try {
      CommandLine cli = cliParser.parse(options, args);
      textify = cli.getOptionValue("textify");
      numThreads = Integer.parseInt(cli.getOptionValue("numThreads"));
      outputFileName = cli.getOptionValue("output");
      printer = new PrintStream(Files.newOutputStream((new File(outputFileName)).toPath()));
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }

    TextPageAsyncHandler[] handlers = new TextPageAsyncHandler[numThreads];

    for (int i = 0; i < handlers.length; i++) {
      handlers[i] = new TextPageAsyncHandler("test", i, CategoryDictionary.dict, TextDictionary.dict, printer);
      handlers[i].start();
    }
    PageHandler<String[], PageFilter<String[]>> handler = new PageMultiHandler(handlers);

    LineNumberReader reader = new LineNumberReader(Files.newBufferedReader((new File(textify)).toPath(), Charset.forName("UTF-8")));
    String line = null;
    String title = null, text = null, category = null;
    line = reader.readLine();
    while (line != null) {
      if (line.equals("title:")) {
        title = reader.readLine();
      }
      if (line.equals("text:")) {
        text = reader.readLine();
      }
      if (line.equals("category:")) {
        category = reader.readLine();
        handler.process(new String[]{title, text, category});
      }
      line = reader.readLine();
    }
    for (int i = 0; i < handlers.length; i++) {
      handlers[i].drain();
      System.out.format("%1$d drained", i);
      handlers[i].join();
    }
  }
}
